/**
 * 
 */
package com.and.xmppsdk.fragment;

import java.util.LinkedList;
import java.util.List;

import org.drl.xmpp.Contact;
import org.drl.xmpp.TSession;
import org.drl.xmpp.storage.table.SessionTable;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.and.xmppsdk.R;
import com.and.xmppsdk.adapter.SessionArrayAdapter;

/**
 * @author hljdrl@gmail.com 会话页面
 * 
 */
public final class SessionFragment extends Fragment implements
		OnItemClickListener {

	private View rootView;
	private ListView mLv;
	private SessionArrayAdapter mAdapter;
	private SessionTable mSessionTable;
	private List<TSession> dataList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSessionTable = SessionTable.getInstance();
		dataList = new LinkedList<TSession>();
	}

	@Override
	public void onResume() {
		super.onResume();
		findData();
	}

	private void findData() {
		if (dataList.size() > 0)
			dataList.clear();
		//
		dataList.addAll(mSessionTable.readAllItems());
		//

		if (mAdapter == null) {
			mAdapter = new SessionArrayAdapter(getActivity(), dataList);
			mLv.setAdapter(mAdapter);
		} else {
			mAdapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		rootView = getView();
		mLv = (ListView) rootView.findViewById(R.id.lst_session);
		mLv.setOnItemClickListener(this);
		registerForContextMenu(mLv);
		findData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.root_ft_session, container, false);
		return view;
	}

	TSession delSessionItem = null;

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v == mLv) { // 判断是哪个ListView
			// 获取当前长按的下标
			int i = ((AdapterContextMenuInfo) menuInfo).position;
			// 具体操作
			delSessionItem = mAdapter.getItem(i);
			menu.setHeaderTitle("选项");
			menu.add(0, Menu.FIRST, Menu.NONE, "删除会话");
		}
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() == Menu.FIRST) {
			mSessionTable.removeSessionItem(delSessionItem);
			findData();
			delSessionItem = null;
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg0 == mLv) {
			TSession mSm = mAdapter.getItem(arg2);
			Contact mCm = new Contact(mSm.getSessionJid());
			Intent mIm = new Intent(getActivity(),
					com.and.xmppsdk.ChatActivity.class);
			mIm.putExtra("session", mSm);
			mIm.putExtra("contact", mCm);
			startActivity(mIm);
		}
	}
}
